<!--通过部门选择用户-->
<template>
  <BasicModal v-bind="$attrs" @register="register" :title="modalTitle" width="1200px" @ok="handleOk" destroyOnClose @visible-change="visibleChange">
    <a-row :gutter="10">
      <a-col :md="7" :sm="24">
        <a-card :style="{ minHeight: '613px', overflow: 'auto' }">
          <!--组织机构-->
          <BasicTree
            ref="treeRef"
            :style="{ minWidth: '250px' }"
            selectable
            @select="onNodeSelect"
            :treeData="treeData"
            :selectedKeys="selectedNodeIds"
            :expandedKeys="expandedKeys"
            :clickRowToExpand="true"
          ></BasicTree>
        </a-card>
      </a-col>
      <a-col :md="17" :sm="24">
        <a-card :style="{ minHeight: '613px', overflow: 'auto' }">
          <!--供应商列表-->
          <BasicTable ref="tableRef" v-bind="getBindValue" :searchInfo="searchInfo" :api="getSupplierPage" :rowSelection="rowSelection"></BasicTable>
        </a-card>
      </a-col>
    </a-row>
  </BasicModal>
</template>
<script lang="ts">
import { defineComponent, unref, ref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicTree } from '/@/components/Tree/index';
import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
import { useSelectBiz } from '/@/components/Form/src/jeecg/hooks/useSelectBiz';
import { useAttrs } from '/@/hooks/core/useAttrs';
import { getSupplierPage } from '/@/api/common/api';
import { loadTree } from '/@/views/purchase/supplierType/supplierType.api';
import { selectProps } from '/@/components/Form/src/jeecg/props/props';
export default defineComponent({
  name: 'UserSelectByDepModal',
  components: {
    //此处需要异步加载BasicTable
    BasicModal,
    BasicTree,
    BasicTable: createAsyncComponent(() => import('/@/components/Table/src/BasicTable.vue'), {
      loading: true,
    }),
  },
  props: {
    ...selectProps,
    //选择框标题
    modalTitle: {
      type: String,
      default: '选择供应商',
    },
  },
  emits: ['register', 'getSelectResult'],
  setup(props, { emit }) {
    const tableRef = ref();
    const treeRef = ref();
    //注册弹框
    const [register, { closeModal }] = useModalInner(async () => {
      await queryTreeData();
    });
    const attrs = useAttrs();
    const treeData = ref([]);
    const selectedNodeIds = ref([]);
    const expandedKeys = ref([]);
    const searchInfo = {};
    /**
     *表格配置
     */
    const tableProps = {
      columns: [
        {
          title: '名称',
          dataIndex: 'name',
          align: 'center',
        },
        {
          title: '联系人',
          dataIndex: 'contact',
          align: 'center',
        },
        {
          title: '联系电话',
          dataIndex: 'contactNumber',
          align: 'center',
        },
        {
          title: '电子邮箱',
          dataIndex: 'email',
          align: 'center',
        },
        {
          title: '累计应付账款',
          dataIndex: 'totalAccountPayment',
          align: 'center',
        },
        {
          title: '地址',
          dataIndex: 'address',
          align: 'center',
        },
      ],
      useSearchForm: true,
      canResize: false,
      showIndexColumn: false,
      striped: true,
      bordered: true,
      size: 'small',
      formConfig: {
        //labelWidth: 200,
        baseColProps: {
          xs: 24,
          sm: 8,
          md: 6,
          lg: 8,
          xl: 6,
          xxl: 10,
        },
        //update-begin-author:liusq date:2023-10-30 for: [issues/5514]组件页面显示错位
        actionColOptions: {
          xs: 24,
          sm: 12,
          md: 12,
          lg: 12,
          xl: 8,
          xxl: 8,
        },
        //update-end-author:liusq date:2023-10-30 for: [issues/5514]组件页面显示错位
        schemas: [
          {
            label: '名称',
            field: 'name',
            component: 'Input',
          },
        ],
        resetFunc: customResetFunc,
      },
    };
    const getBindValue = Object.assign({}, unref(props), unref(attrs), tableProps);
    const [{ rowSelection, visibleChange, indexColumnProps, getSelectResult, reset }] = useSelectBiz(getSupplierPage, getBindValue,emit);
    /**
     * 加载树形数据
     */
    function queryTreeData() {
      loadTree().then((res) => {
        if (res) {
          treeData.value = res;
          // 默认展开父节点
          //expandedKeys.value = unref(treeData).map(item => item.id)
        }
      });
    }
    /**
     * 点击树节点,筛选出对应的供应商
     */
    function onNodeSelect(keys) {
      if (keys[0] != null) {
        if (unref(selectedNodeIds)[0] !== keys[0]) {
          selectedNodeIds.value = [keys[0]];
        }
        searchInfo['typeId'] = unref(selectedNodeIds).join(',');
        tableRef.value.reload();
      }
    }
    /**
     * 自定义重置方法
     * */
    async function customResetFunc() {
      console.log('自定义查询');
      //树节点清空
      selectedNodeIds.value = [];
      //查询条件清空
      searchInfo['typeId'] = '';
      //选择项清空
      reset();
    }
    /**
     * 确定选择
     */
    function handleOk() {
      getSelectResult((options, values) => {
        //回传选项和已选择的值
        emit('getSelectResult', options, values);
        //关闭弹窗
        closeModal();
      });
    }

    return {
      //config,
      handleOk,
      searchInfo,
      register,
      indexColumnProps,
      visibleChange,
      getBindValue,
      rowSelection,

      treeData,
      selectedNodeIds,
      expandedKeys,
      treeRef,
      tableRef,
      getSupplierPage,
      onNodeSelect,
    };
  },
});
</script>

<style scoped lang="less"></style>
